package Map
{
	import BasicEngine.Dispatcher;
	import BasicEngine.LoaderManager;	
	import DataStore.DataSource;	
	import MenuUnit.MenuManager;	
	import SceneUnit.SceneManager;	
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.Loader;
	import flash.events.Event;
	import flash.events.EventDispatcher;
	import flash.geom.Point;
	import flash.geom.Rectangle;
	import flash.net.URLRequest;
	import flash.utils.Dictionary;

	public class MapManager
	{
		//Tiles
		public static var Tiles:Dictionary;
		public static var TilesReady:Boolean = false;
		public static var tileWidth:int;
		public static var tileHeight:int;
		public static var tilesVertical:int;
		public static var tilesHorizontal:int;
		public static var MapSource:BitmapData;
		public static var MapSourceWidth:int;
		public static var MapSourceHeight:int;
		
		public static var Thumbnail:BitmapData;
		public static var MapGrain:Point = new Point( -1, -1);
		
		public static var clrArray:Dictionary = new Dictionary();
		public static var posArray:Dictionary = new Dictionary();
		private static var clr:int;			
		
		public function MapManager()
		{
			
		}
		
		public static function GetFlag():Boolean
		{
			return TilesReady;
		}
		
		//地图管理器初始化
		//载入地图块合成图，资源准备好设置 标志量 TilesReady 为真。
		public static function Init(_tileWidth:int,_tileHeight:int):void
		{
			Tiles = new Dictionary();
			tileWidth = _tileWidth;
			tileHeight = _tileHeight;		
			StartLoadMap(DataSource.sourceMapUrl);
		}
		
		public static function StartLoadMap(_urlImg:String):void
		{
			Dispatcher.RegistTaskUnit(0, 10, 10, 1, "ok", function():void 
			{ 
				var loader:Loader = new Loader();
			    loader.contentLoaderInfo.addEventListener(Event.COMPLETE, FullTiles);
			    loader.load(new URLRequest(_urlImg));
			} );
		}
		
		public static function FullTiles(e:Event):void
		{
			var loader2:Loader = (e.target.loader) as Loader;			
			var bmpData:BitmapData = (loader2.content as Bitmap).bitmapData;
			MapSource = bmpData;
			MapSourceWidth = bmpData.width;
			MapSourceHeight = bmpData.height;
			tilesVertical = MapSourceHeight / tileHeight;
			tilesHorizontal = MapSourceWidth / tileWidth;
				
			var tileWidthNums:int = MapSource.width / tileWidth;
			var tileHeightNums:int = MapSource.height / tileHeight;
			for (var i:int = 0; i < tileWidthNums; i++)
			{
				for (var j:int = 0; j < tileHeightNums; j++)
				{
					var tileBmpData:BitmapData = new BitmapData(tileWidth, tileHeight);
					tileBmpData.copyPixels(bmpData, new Rectangle(i*tileBmpData.width,j*tileBmpData.height, tileBmpData.width, tileBmpData.height), new Point(0, 0));
					var point:Point = new Point(i, j);
					var pos:String = i.toString() + "-" + j.toString();
					Tiles[pos] = tileBmpData;	
					clr += 10;	
					clrArray[pos] = clr;
					posArray[clr] = pos;
				}
			}
		    LoaderManager.imgLoader.removeEventListener(Event.COMPLETE, FullTiles);
			TilesReady = true;
		}
		
		public static  function LoadMap(_url:String):void
		{
			trace(MapManager.TilesReady);
			var loader:Loader = new Loader();
			loader.contentLoaderInfo.addEventListener(Event.COMPLETE, OnParseMap);
			loader.load(new URLRequest(_url));
		}
		
		public static function OnParseMap(e:Event):void
		{
			trace("铺设地图");
			if (!TilesReady)
			{
				trace("地图元素没有准备好,Waiting...");
				Dispatcher.RegistTaskUnit(0, Dispatcher.Clock.SystemTime + 1000, 10, 1, null, function():void
				{
					OnParseMap(e);
				});
				return;
			}
			trace("铺设地图开始");
			var loader2:Loader = (e.target.loader) as Loader;			
			var bmpData:BitmapData = (loader2.content as Bitmap).bitmapData;
			
			Thumbnail = bmpData;
			
			var offsetX:int = 650;
			var offsetY:int = 160;
			var mapImg:BitmapData = ParseMap(Thumbnail);			
			MenuManager.AddImgToMenu(offsetX,offsetY, mapImg);
		}
		
		public static function ParseMap(bmpData:BitmapData):BitmapData
		{
			var mapW:int = bmpData.width * tileWidth;
			var mapH:int = bmpData.height * tileHeight;
			var mapImg:BitmapData = new BitmapData(mapW, mapH, true, 0xffccaa);
			for (var i:int = 0; i < bmpData.width; i++)
			{
				for (var j:int = 0; j < bmpData.height; j++)
				{
					var clr:int = bmpData.getPixel(i, j);
					var pos:String = posArray[clr];
					var tilePos:Point =  new Point(tileWidth * i,tileWidth * j);
					if (clr != 0xffffff)
					{
					   mapImg.copyPixels(Tiles[pos], new Rectangle(0, 0, tileWidth, tileHeight), tilePos);					   
					}					
				}
			}
			return mapImg;
		}
		
		public static function ParseMapSt(_bmpDataName:String):BitmapData
		{
			var bmpData:BitmapData = SceneManager.Thumbnails[_bmpDataName];
			if (bmpData != null)
			{
				return ParseMap(bmpData);
			}		
			else
			return null;
		}
		
		
	}
}